//alert("content-script.js 已经注入");
function ExtractAllValue(){
    const visibleInfoElements = document.querySelectorAll(
        'div.ud__tabs__content > div:nth-child(1) > div > div > div > div > div:nth-child(1) > div > div'
    )
    const inVisibleInfoElements = document.querySelectorAll(
        "div.ud__tabs__content > div:nth-child(1) > div > div > div > div > div.card_edit_hide > div:nth-child(2) > div > div")
    return {...IdentifyField(visibleInfoElements),...IdentifyField(inVisibleInfoElements)}
}


function IdentifyField(elements) {
    let results = {}
    for (let element of elements) {
        const keyElement = element.querySelector('label > span')
        const valueElement = element.querySelector('label > div')
        if(keyElement){
            let key = keyElement.getAttribute('title')
            key = key.replace(/[(（][^)）]*[)）]/g, "")
            results[key] = ExtractValue(valueElement)

            if(key==='项目经理'){
                results[key] = keyElement.nextSibling.querySelector('div span.b-user-tag-text').innerText.trim()
            }
        }

    }
    return results

}

function ExtractValue(element) {
    const allTags = element.querySelectorAll('div.b-select-dropdown-menu')
    let value = ''
    if (allTags.length > 0) {
        let text = ''
        for (let tag of allTags) {
            text = text + tag.querySelector('span').innerText
        }
        value = text
    } else if (element.querySelector('input').getAttribute('value')) {
        value = element.querySelector('input').getAttribute('value')
    } else {
        const valueEle = element.querySelector('div.ace-line>span')
        value = valueEle ? valueEle.innerText : ""
    }
    return value.trim()
}



chrome.extension.onMessage.addListener(
    function (request, sender, sendResponse) {
        sendResponse(JSON.stringify(ExtractAllValue()))
    }
);







